package com.ellricky.baselib.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andr.common.util.ScreenUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.adapter.GridItemDecoration;
import com.ellricky.baselib.adapter.TextGridAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TextGridView extends FrameLayout implements AdapterView.OnItemClickListener {

    private Activity activity;
    private RecyclerView rv;
    private TextGridAdapter adapter;


    public TextGridView(Context context) {
        super(context);
        initView(context);
    }

    public TextGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TextGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        activity = (Activity) context;
        View.inflate(context, R.layout.base_layout_text_grid, this);
        rv = findViewById(R.id.rv_text_grid);

        GridLayoutManager glm = new GridLayoutManager(context, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        rv.setLayoutManager(glm);
        rv.addItemDecoration(new GridItemDecoration((int) ScreenUtil.dpUnit(activity, 10f)
                , (int) ScreenUtil.dpUnit(activity, 30f), android.R.color.transparent));
        rv.setNestedScrollingEnabled(false);

        adapter = new TextGridAdapter();
        adapter.setOnItemClickListener(this);
        rv.setAdapter(adapter);
    }


    public void refreshDataSource(@NonNull List<String> keyList, @NonNull List<String> valueList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        for (int i = 0; i < keyList.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("key", keyList.get(i));
            map.put("value", valueList.get(i));
            dataList.add(map);
        }
        adapter.refresh(dataList);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}
